package com.jr.controller;

import com.jr.entity.User;
import com.jr.service.UserService;
import com.jr.util.UserName;
import org.springframework.beans.factory.NamedBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 用户信息表
 *
 * @author Snoopy
 * @since 1.0.0 2022-12-31
 */
@RestController
public class UserController {

    @Autowired
    private UserService userService;


    /**
     * @description: 首页的登录 从前端取到用户名密码，进行登录验证
     * @author: 金宏飞
     * @date: 2023/1/3 12:09
     * @param: [name, password]
     * @return: boolean
     **/
    @RequestMapping(value = "checkUser",produces = "application/json;charset=utf-8")
    @ResponseBody
    public boolean selectUser(String account, String password, HttpSession session) {
        String phone = "^1[3|4|5|7|8|9][0-9]\\d{8}$";
        String email = "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
        User user = new User();
        if (account.matches(phone)){
            user.setUphone(account);
        }else if (account.matches(email)){
            user.setUemail(account);
        }else {
            user.setUname(account);
        }
        user.setUpassword(password);
        return  userService.loginUser(user,session);
    }

    /**
     * @description: 用户名查重
     * @author: 金宏飞
     * @date: 2023/1/5 10:42
     * @param: [account]
     * @return: java.lang.Boolean
     **/
    @RequestMapping(value = "checkAccount",produces = "application/json;charset=utf-8")
    @ResponseBody
    public Boolean checkAccount(String account) {
        String phone = "^1[3|4|5|7|8|9][0-9]\\d{8}$";
        String email = "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
        User user = new User();
        boolean bl = false;
        if (account.matches(phone)){
            user.setUphone(account);
            bl = true;
        }else if (account.matches(email)){
            user.setUemail(account);
            bl = true;
        }
        if (bl){
            return userService.accountReview(user);
        }else{
            return false;
        }
    }

    /**
     * @description: 注册用户
     * @author: 金宏飞
     * @date: 2023/1/5 12:40
     * @param: [account, password]
     * @return: boolean
     **/
    @RequestMapping(value = "registerUser",produces = "application/json;charset=utf-8")
    @ResponseBody
    public boolean registerUser(String account,String password) {
        String phone = "^1[3|4|5|7|8|9][0-9]\\d{8}$";
        String email = "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
        User user = new User();
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        System.out.println(simpleDateFormat.format(date));
        user.setUid(simpleDateFormat.format(date));
        if (account.matches(phone)){
            user.setUphone(account);
        }else if (account.matches(email)){
            user.setUemail(account);
        }
        user.setUname(new UserName().getStringRandom(5));
        user.setUpassword(password);
        return  userService.registerUser(user);
    }

    /**
     * @description: 根据id查询个人用户的全部信息
     * @author: 王子华
     * @date: 2023/1/3 9:48
     * @param:
     * @return:
     **/
    @RequestMapping(value = "getuserbyid",produces = "application/json;charset=utf-8")
    @ResponseBody
    public User selectUser(String uid) {
        User user = new User();
        user.setUid(uid);
        User user1=userService.getUserByUid(user);
        //当日期部位空时
        if(user1.getUbirthday()!=null){
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            user1.setUbirthdaystr(simpleDateFormat.format(user1.getUbirthday()));
            user1.setUgraduationtimestr(simpleDateFormat.format(user1.getUgraduationtime()));
        }
        return user1;
    }

    /**
     * @description:修改个人信息
     * @author: 王子华
     * @date: 2023/1/3 10:16
     * @param:
     * @return:
     **/
    @RequestMapping(value = "updateuserinfo")
    @ResponseBody
    public void updateUserById(User user,HttpSession session) throws ParseException {
        if(user.getUbirthdaystr()!=""  &&user.getUgraduationtimestr()!="") {
            SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
            Date ubirthday = ft.parse( user.getUbirthdaystr());
            Date ugraduationtime = ft.parse( user.getUgraduationtimestr());
            user.setUbirthday(ubirthday);
            user.setUgraduationtime(ugraduationtime);
        }
        userService.updateUserinfoByUid(user);
        if (user.getUphone()==null || user.getUphone()==""){
            String account=user.getUemail();
            String password = user.getUpassword();
            System.out.println(account);
            selectUser(account,password,session);
        }else {
            String account=user.getUphone();
            System.out.println(account);
            String password = user.getUpassword();
            selectUser(account,password,session);
        }


    }

    @PostMapping(value = "updatePassword")
    public @ResponseBody String updatePassword(User user){
        if(userService.changeUserInfo(user))
            return "true";
        return "false";

    }

    @PostMapping(value = "queryusername",produces = "application/json;charset=utf-8;")
    public @ResponseBody User getUserName(User user){
        User user1 = userService.queryUser(user);
        if (user1!=null)
            return user1;
        return user;
    }

}